{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名 文华虎  \n",
    "+ 学号 0165180"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "彩虹 - 周杰伦\n",
      "哪里有彩虹告诉我\n",
      "能不能把我的愿望还给我\n",
      "为什么天这么安静\n",
      "所有的云都跑到我这里\n",
      "有没有口罩一个给我\n",
      "释怀说了太多就成真不了\n",
      "也许时间是一种解药\n",
      "也是我现在正服下的毒药\n",
      "\n",
      "看不见你的笑 我怎么睡得着\n",
      "你的身影这么近我却抱不到\n",
      "没有地球太阳还是会绕\n",
      "没有理由我也能自己走\n",
      "你要离开 我知道很简单\n",
      "你说依赖 是我们的阻碍\n",
      "就算放开 那能不能别没收我的爱\n",
      "当作我最后才明白\n",
      "\n",
      "有没有口罩一个给我\n",
      "释怀说了太多就成真不了\n",
      "也许时间是一种解药\n",
      "也是我现在正服下的毒药\n",
      "\n",
      "看不见你的笑 我怎么睡得着\n",
      "你的身影这么近我却抱不到\n",
      "没有地球太阳还是会绕\n",
      "没有理由我也能自己走\n",
      "你要离开 我知道很简单\n",
      "你说依赖 是我们的阻碍\n",
      "就算放开 那能不能别没收我的爱\n",
      "当作我最后才明白\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def chorus():\n",
    "    print(\"看不见你的笑 我怎么睡得着\\n你的身影这么近我却抱不到\\n没有地球太阳还是会绕\\n没有理由我也能自己走\\n你要离开 我知道很简单\\n你说依赖 是我们的阻碍\\n就算放开 那能不能别没收我的爱\\n当作我最后才明白\\n\")\n",
    "print(\"彩虹 - 周杰伦\\n哪里有彩虹告诉我\\n能不能把我的愿望还给我\\n为什么天这么安静\\n所有的云都跑到我这里\\n有没有口罩一个给我\\n释怀说了太多就成真不了\\n也许时间是一种解药\\n也是我现在正服下的毒药\\n\")\n",
    "chorus()\n",
    "print(\"有没有口罩一个给我\\n释怀说了太多就成真不了\\n也许时间是一种解药\\n也是我现在正服下的毒药\\n\")\n",
    "chorus()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean = 6.31\n",
      "sample variance = 2.46\n",
      "n=13\n",
      "mean = 16.55\n",
      "sample variance = 13.90\n",
      "n=11\n"
     ]
    }
   ],
   "source": [
    "def function_statistics(*arg):\n",
    "    a=0\n",
    "    n=0\n",
    "    c=0\n",
    "    for value in arg:\n",
    "        a=a+value\n",
    "        n+=1\n",
    "        mean=a/n\n",
    "        c=c+(value-a/n)**2\n",
    "        variance=c/n\n",
    "    print(\"mean = %.2f\" %mean)\n",
    "    print(\"sample variance = %.2f\" %variance)\n",
    "    print(\"n=%d\" %n)\n",
    "\n",
    "function_statistics(5,4,7,9,6,7,8,5,3,5,7,8,8)\n",
    "function_statistics(11,12,13,14,15,16,17,18,20,22,24)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算样本方差请输入1,计算样本标准差请输入2：\n",
      "1\n",
      "mean = 6.31\n",
      "sample variance = 2.46\n",
      "n=13\n",
      "计算样本方差请输入1,计算样本标准差请输入2：\n",
      "2\n",
      "mean = 16.55\n",
      "sample standard deviation = 3.73\n",
      "n=11\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def function_statistics(*arg):\n",
    "    a=0\n",
    "    n=0\n",
    "    c=0\n",
    "    for value in arg:\n",
    "        a=a+value\n",
    "        n+=1\n",
    "        mean=a/n\n",
    "        c=c+(value-a/n)**2\n",
    "        variance=c/n\n",
    "        \n",
    "    print(\"计算样本方差请输入1,计算样本标准差请输入2：\")\n",
    "    d=int(input())\n",
    "    if d==1:\n",
    "        print(\"mean = %.2f\" %mean)\n",
    "        print(\"sample variance = %.2f\" %variance)\n",
    "        print(\"n=%d\" %n)\n",
    "    if d==2:\n",
    "        print(\"mean = %.2f\" %mean)\n",
    "        print(\"sample standard deviation = %.2f\" %math.sqrt(variance))\n",
    "        print(\"n=%d\" %n)\n",
    "    \n",
    "\n",
    "function_statistics(5,4,7,9,6,7,8,5,3,5,7,8,8)\n",
    "function_statistics(11,12,13,14,15,16,17,18,20,22,24)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算样本方差请输入1,计算样本标准差请输入2：\n",
      "1\n",
      "计算均值的标准误、最大值、最小值请输入1，不计算请输入2：\n",
      "1\n",
      "mean = 6.31\n",
      "sample variance = 2.46\n",
      "n=13\n",
      "standard error = 0.12\n",
      "max = 9\n",
      "min = 3\n",
      "计算样本方差请输入1,计算样本标准差请输入2：\n",
      "1\n",
      "计算均值的标准误、最大值、最小值请输入1，不计算请输入2：\n",
      "2\n",
      "mean = 16.55\n",
      "sample variance = 13.90\n",
      "n=11\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def function_statistics(*arg):\n",
    "    a=0\n",
    "    n=0\n",
    "    c=0\n",
    "    for value in arg:\n",
    "        a=a+value\n",
    "        n+=1\n",
    "        mean=a/n\n",
    "        c=c+(value-a/n)**2\n",
    "        variance=c/n\n",
    "        f=c/(n**3)\n",
    "    print(\"计算样本方差请输入1,计算样本标准差请输入2：\")\n",
    "    d=int(input())\n",
    "    print(\"计算均值的标准误、最大值、最小值请输入1，不计算请输入2：\")\n",
    "    e=int(input())\n",
    "    if d==1:\n",
    "        print(\"mean = %.2f\" %mean)\n",
    "        print(\"sample variance = %.2f\" %variance)\n",
    "        print(\"n=%d\" %n)\n",
    "    if d==2:\n",
    "        print(\"mean = %.2f\" %mean)\n",
    "        print(\"sample standard deviation = %.2f\" %math.sqrt(variance))\n",
    "        print(\"n=%d\" %n)\n",
    "    if e==1:\n",
    "        print(\"standard error = %.2f\"%math.sqrt(f))\n",
    "        print(\"max = %d\"%max(arg))\n",
    "        print(\"min = %d\"%min(arg))\n",
    "    \n",
    "\n",
    "function_statistics(5,4,7,9,6,7,8,5,3,5,7,8,8)\n",
    "function_statistics(11,12,13,14,15,16,17,18,20,22,24)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
